package io.renren.modules.withdraw_cash.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.withdraw_cash.dao.V9WithdrawCashDao;
import io.renren.modules.withdraw_cash.entity.V9WithdrawCashEntity;
import io.renren.modules.withdraw_cash.entity.params.V9WithdrawCashParams;
import io.renren.modules.withdraw_cash.service.V9WithdrawCashService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.Map;

@Service("v9WithdrawCashService")
public class V9WithdrawCashServiceImpl extends ServiceImpl<V9WithdrawCashDao, V9WithdrawCashEntity> implements V9WithdrawCashService {

    private static Logger logger = LoggerFactory.getLogger(V9WithdrawCashServiceImpl.class);

    @Autowired
    V9WithdrawCashDao v9WithdrawCashDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String wc_order_sn = (String)params.get("wc_order_sn");
        String special_id = (String)params.get("special_id");
        String status = (String) params.get("status");
        Long startInt = 0l;
        if (params.get("start") != null){
            String start = params.get("start").toString();
            Date startTime = DateUtils.string2Date(start);
            startInt =  startTime.getTime() / 1000;
        }
        Long endInt = 0l;
        if (params.get("end") != null){
            String end = params.get("end").toString();
            Date endTime = DateUtils.string2Date(end);
            endInt = endTime.getTime() / 1000;
        }
        Integer status1=null;
        if (StringUtils.isNotBlank(status)) {
            status1=Integer.parseInt(status);
        }
        IPage<V9WithdrawCashEntity> page = this.page(
                new Query<V9WithdrawCashEntity>().getPage(params,"wc_id",false),
                new QueryWrapper<V9WithdrawCashEntity>()
                        .like(StringUtils.isNotBlank(wc_order_sn), "wc_order_sn", "%"+wc_order_sn+"%")
                        .like(StringUtils.isNotBlank(special_id), "special_id", "%"+special_id+"%")
                        .eq(status1!=null, "status", status)
                        .ge(startInt != 0,"wc_addtime",startInt)
                        .le(endInt != 0,"wc_addtime",endInt)
        );
        return new PageUtils(page);
    }

    @Override
    public R pass(V9WithdrawCashParams params) {
        V9WithdrawCashEntity byId = this.getById(params.getId());
        if (byId.getStatus()=="2"&&"2".equals(byId.getStatus()) || byId.getStatus()=="3" && "3".equals(byId.getStatus())){
            return R.error("此申请已通过或退回,不能再次操作");
        }
        if (params.getType() == 1){
            v9WithdrawCashDao.pass(params.getId());
            byId.setWcAudittime((int) Instant.now().getEpochSecond());
        }
        if (params.getType() == 2){
            v9WithdrawCashDao.returned(params.getId());
            byId.setWcAudittime((int) Instant.now().getEpochSecond());
        }
        return R.ok();
    }

}
